package lancs.mobilemedia.core.ui.controller.commands;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.rms.RecordStoreFullException;

import lancs.mobilemedia.core.ui.controller.AbstractController;
import lancs.mobilemedia.core.ui.controller.AlbumController;
import lancs.mobilemedia.core.ui.controller.ControllerCommand;
import lancs.mobilemedia.core.ui.screens.NewLabelScreen;
import lancs.mobilemedia.lib.exceptions.InvalidPhotoAlbumNameException;
import lancs.mobilemedia.lib.exceptions.PersistenceMechanismException;

public class SaveAlbum implements ControllerCommand {

	public boolean execute(AbstractController controller) {
		try {
			if (controller.getCurrentScreen() instanceof NewLabelScreen) {
				NewLabelScreen currentScreen = (NewLabelScreen) controller.getCurrentScreen();
				if (currentScreen.getFormType() == NewLabelScreen.NEW_ALBUM)
					controller.getAlbumData().createNewPhotoAlbum(currentScreen.getLabelName());
				else if (currentScreen.getFormType() == NewLabelScreen.LABEL_PHOTO) {

				}
			}
		} catch (PersistenceMechanismException e) {
			Alert alert = null;
			if (e.getCause() instanceof  RecordStoreFullException)
				alert = new Alert( "Error", "The mobile database is full", null, AlertType.ERROR);
			else
				alert = new Alert( "Error", "The mobile database can not add a new photo album", null, AlertType.ERROR);
			Display.getDisplay(controller.getMidlet()).setCurrent(alert, Display.getDisplay(controller.getMidlet()).getCurrent());
			return true;
		} catch (InvalidPhotoAlbumNameException e) {
			Alert alert = new Alert( "Error", "You have provided an invalid Photo Album name", null, AlertType.ERROR);
			Display.getDisplay(controller.getMidlet()).setCurrent(alert, Display.getDisplay(controller.getMidlet()).getCurrent());
			return true;
		}
		((AlbumController) controller).goToPreviousScreen();
		return true;
	}
}